package com.jcrj.remoteclient.util;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.jcrj.remoteclient.ui.MainActivity;
import com.jcrj.remoteclient.ui.R;

/**
 * 应用程序UI工具包：封装UI相关的一些操作
 */
public class UIHelper {

	public final static int LISTVIEW_ACTION_INIT = 0x01; // 初始化
	public final static int LISTVIEW_ACTION_REFRESH = 0x02; // 刷新
	public final static int LISTVIEW_ACTION_SCROLL = 0x03; // 滑动
	public final static int LISTVIEW_ACTION_CHANGE_CATALOG = 0x04; //

	public final static int LISTVIEW_DATA_MORE = 0x01; // listView还有更多
	public final static int LISTVIEW_DATA_LOADING = 0x02; // 加载中
	public final static int LISTVIEW_DATA_FULL = 0x03; // 全部加载
	public final static int LISTVIEW_DATA_EMPTY = 0x04; // 没有列表信息

	public final static int REQUEST_CODE_FOR_RESULT = 0x01;
	public final static int REQUEST_CODE_FOR_REPLY = 0x02;

	/**
	 * 回到首页
	 */
	public static void showHome(Activity activity) {
		Intent intent = new Intent(activity, MainActivity.class);
		activity.startActivity(intent);
		activity.finish();
	}

	/**
	 * 打开指定的Activity
	 * 
	 * @param context
	 * @param clazz
	 */
	@SuppressWarnings("rawtypes")
	public static void openActivity(Activity context, Class clazz) {
		openActivity(context, clazz, null);
	}

	/**
	 * 关闭当前Activity,打开指定的Activity
	 * 
	 * @param context
	 * @param clazz
	 */
	@SuppressWarnings("rawtypes")
	public static void openActivityNoPre(Activity context, Class clazz) {
		openActivityNoPre(context, clazz, null);
	}

	/**
	 * 打开指定的Activity,并传递参数
	 * 
	 * @param context
	 * @param clazz
	 * @param map
	 */
	@SuppressWarnings("rawtypes")
	public static void openActivity(Activity context, Class clazz,
			Map<String, Serializable> map) {
		Intent intent = new Intent(context, clazz);
		if (map != null && map.size() > 0) {
			for (Entry<String, Serializable> en : map.entrySet()) {
				intent.putExtra(en.getKey(), en.getValue());
			}
		}
		context.startActivity(intent);

	}

	/**
	 * 关闭当前Activity,打开指定的Activity,并传递参数
	 * 
	 * @param context
	 * @param clazz
	 * @param map
	 */
	@SuppressWarnings("rawtypes")
	public static void openActivityNoPre(Activity context, Class clazz,
			Map<String, Serializable> map) {
		Intent intent = new Intent(context, clazz);
		if (map != null && map.size() > 0) {
			for (Entry<String, Serializable> en : map.entrySet()) {
				intent.putExtra(en.getKey(), en.getValue());
			}
		}
		context.startActivity(intent);
		context.finish();
	}

	/** 全局web样式 */
	public final static String WEB_STYLE = "<style>* {font-size:16px;line-height:20px;} p {color:#333;} a {color:#3E62A6;} img {max-width:310px;} "
			+ "img.alignleft {float:left;max-width:120px;margin:0 10px 5px 0;border:1px solid #ccc;background:#fff;padding:2px;} "
			+ "pre {font-size:9pt;line-height:12pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;} "
			+ "a.tag {font-size:15px;text-decoration:none;background-color:#bbd6f3;border-bottom:2px solid #3E6D8E;border-right:2px solid #7F9FB6;color:#284a7b;margin:2px 2px 2px 0;padding:2px 4px;white-space:nowrap;}</style>";

	/**
	 * 弹出Toast消息
	 * 
	 * @param msg
	 */
	public static void ToastMessage(Context cont, String msg) {
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}

	public static void ToastMessage(Context cont, int msg) {
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}

	public static void ToastMessage(Context cont, String msg, int time) {
		Toast.makeText(cont, msg, time).show();
	}

	/**
	 * 点击返回监听事件
	 * 
	 * @param activity
	 * @return
	 */
	public static View.OnClickListener finish(final Activity activity) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				activity.finish();
			}
		};
	}

	/**
	 * 退出程序
	 * 
	 * @param cont
	 */
	public static void Exit(final Context cont) {
		AlertDialog.Builder builder = new AlertDialog.Builder(cont);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle(R.string.app_menu_surelogout);
		builder.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// 退出
						AppManager.getAppManager().AppExit(cont);
					}
				});
		builder.setNegativeButton(R.string.cancle,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		/*
		 * AlertDialog dialog = builder.create(); Window window =
		 * dialog.getWindow(); window.setGravity(Gravity.BOTTOM);
		 * WindowManager.LayoutParams lp = window.getAttributes(); lp.width =
		 * LayoutParams.FILL_PARENT; window.setAttributes(lp); dialog.show();
		 */
		builder.show();
	}

	/**
	 * 发送App异常崩溃报告
	 * 
	 * @param cont
	 * @param crashReport
	 */
	public static void sendAppCrashReport(final Context cont,
			final String crashReport) {
		AlertDialog.Builder builder = new AlertDialog.Builder(cont);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle(R.string.app_error);
		builder.setMessage(R.string.app_error_message);
		builder.setPositiveButton(R.string.submit_report,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// 发送异常报告
						Intent i = new Intent(Intent.ACTION_SEND);
						// i.setType("text/plain"); //模拟器
						i.setType("message/rfc822"); // 真机
						i.putExtra(Intent.EXTRA_EMAIL,
								new String[] { "356179724@qq.com" });
						i.putExtra(Intent.EXTRA_SUBJECT,
								"宣州开发区OA Android客户端 - 错误报告");
						i.putExtra(Intent.EXTRA_TEXT, crashReport);
						System.out.println(crashReport);
						cont.startActivity(Intent.createChooser(i, "发送错误报告"));
						// 退出
						AppManager.getAppManager().AppExit(cont);
					}
				});
		builder.setNegativeButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// 退出
						AppManager.getAppManager().AppExit(cont);
					}
				});
		builder.show();
	}
}
